﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Snake1._0
{
    public partial class FormMain : Form
    {
        private Palette p;
        //定义画布长宽，以及每个蛇块的大小
        private int width = 20;
        private int height = 20;
        private int size = 15;

        private delegate void InvokeCallback(string msg);
        void m_comm_MessageEvent(string msg)

        {

            if (labelCount.InvokeRequired)

            {

                labelCount.Invoke(new InvokeCallback(m_comm_MessageEvent), new object[] { msg });

            }

            else

            {

                labelCount.Text = msg;

            }

        }



        public FormMain()
        {
            InitializeComponent();
            // 加入这行 这是最简单的办法， 相当于不检查线程之间的冲突，允许各个线程随便乱搞，最后Lable1控件的值是什么就难以预料了 (不推荐使用这种方法)
            //Control.CheckForIllegalCrossThreadCalls = false;
        }

        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// 初始化
        /// </summary>
        public void FormMain_Load(object sender, EventArgs e)
        {
            //画布大小
            this.pictureBox.Width = width * size;
            this.pictureBox.Height = height * size;
            //窗体大小
            this.Width = pictureBox.Width + 30;
            this.Height = pictureBox.Height + 80;

            
        }

        /// <summary>
        /// 按键事件（只能90度拐弯，不能180度拐弯）
        /// </summary>
        public void FormMain_KeyDown(object sender, KeyEventArgs e)
        {
            if((e.KeyCode==Keys.W|| e.KeyCode == Keys.Up)&&p.Direction!=Direction.Down)
            {
                p.Direction = Direction.Up;
                return;
            }
            if ((e.KeyCode == Keys.D || e.KeyCode == Keys.Right) && p.Direction != Direction.Left)
            {
                p.Direction = Direction.Right;
                return;
            }
            if ((e.KeyCode == Keys.S || e.KeyCode == Keys.Down) && p.Direction != Direction.Up)
            {
                p.Direction = Direction.Down;
                return;
            }
            if ((e.KeyCode == Keys.A || e.KeyCode == Keys.Left) && p.Direction != Direction.Right)
            {
                p.Direction = Direction.Left;
                return;
            }
        }

        /// <summary>
        /// 画布
        /// </summary>
        public void pictureBox_Paint(object sender, PaintEventArgs e)
        {
            if(p!=null)
            {
                p.PaintPalette(e.Graphics);
            }
        }

        private void 开始ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if(p!=null)
            {
                if (p.IsStop)
                {
                    p.Start();
                }
                if(p.IsGameOver)
                {
                    p = new Palette(width, height, size, this.pictureBox.BackColor, Graphics.FromHwnd(this.pictureBox.Handle), 1, Label.FromChildHandle(this.labelCount.Handle), Label.FromChildHandle(this.labelLevel.Handle));
                    p.Start();
                }
            }
            else
            {
                p = new Palette(width, height, size, this.pictureBox.BackColor, Graphics.FromHwnd(this.pictureBox.Handle), 1,Label.FromChildHandle(this.labelCount.Handle),Label.FromChildHandle(this.labelLevel.Handle));
                p.Start();
            }
            
            
            
        }

        private void 暂停ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if(p!=null)
            {
                if(!p.IsGameOver)
                {
                    p.Stop();
                }
            }
            
        }
    }
}
